from pydantic_settings import BaseSettings
from typing import Optional


class Settings(BaseSettings):
    """应用配置"""
    
    # 应用基础配置
    APP_NAME: str = "AI法考备考软件"
    APP_VERSION: str = "0.1.0"
    API_V1_PREFIX: str = "/api/v1"
    DEBUG: bool = True
    
    # 服务器配置
    BACKEND_HOST: str = "0.0.0.0"
    BACKEND_PORT: int = 8000
    
    # Redis配置
    REDIS_HOST: str = "localhost"
    REDIS_PORT: int = 6379
    REDIS_DB: int = 0
    
    # Neo4j配置
    NEO4J_URI: str = "bolt://localhost:7687"
    NEO4J_USER: str = "neo4j"
    NEO4J_PASSWORD: str = "password"
    
    # PostgreSQL配置（可选）
    POSTGRES_HOST: str = "localhost"
    POSTGRES_PORT: int = 5432
    POSTGRES_DB: str = "ailawer"
    POSTGRES_USER: str = "ailawer"
    POSTGRES_PASSWORD: str = "password"
    
    # 安全配置
    SECRET_KEY: str = "your-secret-key-here-change-in-production"
    ALGORITHM: str = "HS256"
    ACCESS_TOKEN_EXPIRE_MINUTES: int = 30
    
    # AI服务配置
    OPENAI_API_KEY: Optional[str] = None
    OPENAI_BASE_URL: Optional[str] = None  # OpenAI API基础URL（用于代理）
    ANTHROPIC_API_KEY: Optional[str] = None
    
    # 国内大模型配置
    TONGYI_API_KEY: Optional[str] = None  # 通义千问API密钥
    WENXIN_API_KEY: Optional[str] = None  # 文心一言API密钥
    ZHIPU_API_KEY: Optional[str] = None  # 智谱AI API密钥
    ZHIPU_BASE_URL: Optional[str] = None  # 智谱AI API基础URL（用于自定义端点）
    
    # 大模型提供商选择
    LLM_PROVIDER: Optional[str] = None  # 指定使用的大模型提供商（openai/tongyi/wenxin/zhipu/anthropic）
    
    # 多模型切换策略
    LLM_SWITCH_MODE: str = "fallback"  # 切换模式：fallback（故障转移，按优先级自动切换）或 round_robin（轮询）
    LLM_PROVIDER_PRIORITY: Optional[str] = None  # 自定义优先级，用逗号分隔，如 "openai,zhipu,tongyi"
    
    # Legal-BERT配置
    LEGAL_BERT_MODEL_NAME: str = "hfl/chinese-bert-wwm-ext"  # 中文BERT模型，备选：thunlp/Lawformer_zh
    LEGAL_BERT_DEVICE: str = "auto"  # auto/cpu/cuda，auto会自动检测
    LEGAL_BERT_MAX_LENGTH: int = 512  # 最大输入长度
    LEGAL_BERT_ENABLE_CACHE: bool = True  # 是否启用分析结果缓存
    
    # 爬虫配置
    ZHUMA_USERNAME: Optional[str] = None  # 竹马法考用户名（从环境变量读取）
    ZHUMA_PASSWORD: Optional[str] = None  # 竹马法考密码（从环境变量读取）
    SCRAPER_REQUEST_DELAY: float = 1.0  # 请求间隔（秒）
    SCRAPER_MAX_RETRIES: int = 3  # 最大重试次数
    SCRAPER_TIMEOUT: int = 30  # 请求超时时间（秒）
    
    class Config:
        env_file = ".env"
        case_sensitive = True


settings = Settings()

